/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.ppm.sim.impl.mas;

import bgu.ppm.sim.api.mas.MailBox;
import bgu.ppm.sim.api.mas.Mailer;
import bgu.ppm.sim.api.mas.Message;
import bgu.ppm.sim.api.mas.MultiAgentSystem;
import bgu.sonar.util.clone.DeepCopy;

/**
 *
 * @author User
 */
public class SimpleMailer implements Mailer {

    private SimpleMailbox[] mailboxes;
    private boolean cloneMessages = true;

    @Override
    public void send(Message message, int to) {
        if (cloneMessages) {
            message = DeepCopy.copy(message);
        }

        mailboxes[to].pushMessage(message);
    }

    @Override
    public MailBox getMailbox(int agent) {
        return mailboxes[agent];
    }

    @Override
    public void initialize(MultiAgentSystem system) {
        mailboxes = new SimpleMailbox[system.getGlobalModel().getNumberOfAgents()];
        for (int i = 0; i < mailboxes.length; i++) {
            mailboxes[i] = new SimpleMailbox();
        }
    }

    public boolean isCloneMessages() {
        return cloneMessages;
    }

    public void setCloneMessages(boolean cloneMessages) {
        this.cloneMessages = cloneMessages;
    }
}
